package com.chanzany.principle.demeter.improve;



import java.util.ArrayList;
import java.util.List;

public class Demeter {
    public static void main(String[] args) {
        SchoolManager schoolManager = new SchoolManager();
        schoolManager.printAllEmployee(new CollegeManager());
    }
}

//总部的员工
class Employee{
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

//学院的员工
class CollegeEmployee{
    private String id;
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }
}

//学院管理类
class CollegeManager{
    public List<CollegeEmployee> collegeEmployees(){
        ArrayList<CollegeEmployee> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("学院员工ID="+i);
            list.add(emp);
        }
        return list;
    }
    public void printAll(){
        List<CollegeEmployee> collegeEmployees = this.collegeEmployees();
        System.out.println("学院员工:");
        collegeEmployees.forEach(a->System.out.println(a.getId()));
    }
}

/**
 * 学校管理类
 * 直接朋友：Employee, CollegeManager
 * CollegeEmployee不是直接朋友并出现在了局部变量中，这样违反了迪米特法则
 * 解决方案:将输出学院员工的方法，封装到CollegeManager中
*/
class SchoolManager{
    public List<Employee> employees(){
        ArrayList<Employee> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工ID="+i);
            list.add(emp);
        }
        return list;
    }
    void printAllEmployee(CollegeManager sub){
        sub.printAll();
        List<Employee> employees = this.employees();
        System.out.println("学校总部员工:");
        employees.forEach(a->System.out.println(a.getId()));

    }
}

